package com.example.aixiangshou.runninggame;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;

import com.x_memteor.base_master.utils.TextViewLinkSpanUtil;

import java.util.ArrayList;
import java.util.List;

public class SignInDetalActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText mEtName;
    private EditText mEtMail;
    /**
     * FEMALE
     */
    private RadioButton mButFemale;
    /**
     * MALE
     */
    private RadioButton mButMale;
    private Spinner mSpiMonth;
    private Spinner mSpiDay;
    private Spinner mSpiYear;
    /**
     * PAIS
     */
    private TextView mTvPais;
    private TextView mTvContent;
    private Spinner mSpiPais;
    private ImageView mIvCheck;
    /**
     * A C E P T
     */
    private TextView mTvCheck;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in_detal);
        initView();
        initDate();
    }

    private void initDate() {
        initMonthSpinner();
        initDaySpinner();
        initYearSpinner();
        initPaisSpinner();
        initText();

    }

    private void initMonthSpinner() {
        //数据
        List<String> data_list = new ArrayList<String>();

        data_list.add("MONTH");
        for (int i = 1; i < 13; i++) {
            data_list.add(i + "");
        }
        //适配器
        simpleArrayAdapter arrAdapter = new simpleArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, data_list);
        //设置样式
        arrAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        mSpiMonth.setAdapter(arrAdapter);
        mSpiMonth.setSelection(0, true);
    }

    private void initDaySpinner() {
        //数据
        List<String> data_list = new ArrayList<String>();
        data_list.add("DAY");
        for (int i = 1; i < 31; i++) {
            data_list.add(i + "");
        }
        //适配器
        simpleArrayAdapter arrAdapter = new simpleArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, data_list);
        //设置样式
        arrAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        mSpiDay.setAdapter(arrAdapter);
        mSpiDay.setSelection(0, true);
    }

    private void initYearSpinner() {
        //数据
        List<String> data_list = new ArrayList<String>();
        data_list.add("YEAR");
        for (int i = 2015; i < 2100; i++) {
            data_list.add(i + "");
        }
        //适配器
        simpleArrayAdapter arrAdapter = new simpleArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, data_list);
        //设置样式
        arrAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        mSpiYear.setAdapter(arrAdapter);
        mSpiYear.setSelection(0, true);
    }

    private void initPaisSpinner() {
        //数据
        List<String> data_list = new ArrayList<String>();
        data_list.add("PAIS");
        for (int i = 1; i < 10; i++) {
            data_list.add(i + "");
        }
        //适配器
        simpleArrayAdapter arrAdapter = new simpleArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, data_list);
        //设置样式
        arrAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        mSpiPais.setAdapter(arrAdapter);
        mSpiPais.setSelection(0, true);
    }

    private void initView() {
        mEtName = (EditText) findViewById(R.id.et_name);
        mEtMail = (EditText) findViewById(R.id.et_mail);
        mButFemale = (RadioButton) findViewById(R.id.but_female);
        mButMale = (RadioButton) findViewById(R.id.but_male);
        mSpiMonth = (Spinner) findViewById(R.id.spi_month);
        mSpiDay = (Spinner) findViewById(R.id.spi_day);
        mSpiYear = (Spinner) findViewById(R.id.spi_year);
        mTvPais = (TextView) findViewById(R.id.tv_pais);
        mTvContent = (TextView) findViewById(R.id.tv_content);
        mSpiPais = (Spinner) findViewById(R.id.spi_pais);
        mTvCheck = (TextView) findViewById(R.id.tv_check);
        mTvCheck.setOnClickListener(this);
    }

    private void initText() {
        SpannableStringBuilder builder = new SpannableStringBuilder();

        String string1 = getString(R.string.text_sign_deal_1);
        String string2 = getString(R.string.text_sign_deal_2);
        String string3 = getString(R.string.text_sign_deal_3);
        String string4 = getString(R.string.text_sign_deal_4);

        builder.append(string1)
                .append(string2)
                .append(string3)
                .append(string4);

        mTvContent.setText(builder);

//        //设置文字颜色Span
//        ForegroundColorSpan Span1 = new ForegroundColorSpan(Color.BLACK);
//        ForegroundColorSpan Span2 = new ForegroundColorSpan(Color.BLUE);
//
//        //设置超链接Span
//        URLSpan Span3 = new URLSpan("wwww.baidu.com");
//        //设置文本样式之字体Span
//        AbsoluteSizeSpan Span4 = new AbsoluteSizeSpan(80);
//        //设置删除线Span
//        StrikethroughSpan Span5 = new StrikethroughSpan();
//        //设置下划线Span
//        UnderlineSpan Span6 = new UnderlineSpan();
//
//        builder.setSpan(Span1, 0, string1.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        builder.setSpan(Span2, string1.length(), string1.length() + string2.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        builder.setSpan(Span1, string1.length() + string2.length(), string1.length() + string2.length() + string3.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        builder.setSpan(Span2, string1.length() + string2.length() + string3.length(), string1.length() + string2.length() + string3.length() + string4.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        String html = string1 +
                " <a href='http://www.baidu.com'>" + string2 + "</a> " + string3 +
                " <a href='http://www.google.com'>" + string4 + "</a>";
        mTvContent.setText(builder);
        TextViewLinkSpanUtil.textLinkClick(this, html, mTvContent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.tv_check:
                startActivity(new Intent(SignInDetalActivity.this, WelcomeActivity.class));
                break;
        }
    }
}
